package com.zuo.seckilldemo.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zuo.seckilldemo.entity.User;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserUtil {
    public static void main(String[] args) throws Exception {
        createUser(100);
    }
    private static void createUser(int count) throws Exception{
        List<User> users = new ArrayList<User>(count);
        for (int i = 0; i < count; i++) {
            User user = new User();
            user.setMobileNumber(10000000000L+i);
            user.setNickname("user"+i);
            user.setSalt("1a2b3c");
            user.setPassword(MD5Util.frontEndPassToDB("123456",user.getSalt()));
            Date date = new Date();
            user.setRegisterDate(new Timestamp(date.getTime()));
            user.setLoginCount(1);
            users.add(user);
        }
        System.out.println("create user");
//        Connection conn = getConn();
//        String sql = "insert into sk_user(mobile_number,nickname,password,salt,register_date,login_count)values(?,?,?,?,?,?)";
//        PreparedStatement statement = conn.prepareStatement(sql);
//        for (int i = 0; i < users.size(); i++) {
//            User user = users.get(i);
//            statement.setLong(1,user.getMobileNumber());
//            statement.setString(2,user.getNickname());
//            statement.setString(3,user.getPassword());
//            statement.setString(4,user.getSalt());
//            statement.setTimestamp(5,user.getRegisterDate());
//            statement.setInt(6,user.getLoginCount());
//            statement.addBatch();
//        }
//        statement.executeBatch();
//        statement.close();
//        conn.close();
//        System.out.println("insert to db");
//        登录，生成token
        String urlString = "http://localhost:8080/login/do_login";
        File file = new File("D:/tokens.txt");
        if(file.exists()) {
            file.delete();
        }
        RandomAccessFile raf = new RandomAccessFile(file, "rw");
        file.createNewFile();
        raf.seek(0);
        for(int i=0;i<users.size();i++) {
            User user = users.get(i);
            URL url = new URL(urlString);
            HttpURLConnection co = (HttpURLConnection)url.openConnection();
            co.setRequestMethod("POST");
            co.setDoOutput(true);
            OutputStream out = co.getOutputStream();
            String params = "mobile_number="+user.getMobileNumber()+"&password=123456";
            out.write(params.getBytes());
            out.flush();
            InputStream inputStream = co.getInputStream();
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            byte buff[] = new byte[1024];
            int len = 0;
            while((len = inputStream.read(buff)) >= 0) {
                bout.write(buff, 0 ,len);
            }
            inputStream.close();
            bout.close();
            String response = new String(bout.toByteArray());
            JSONObject jo = JSON.parseObject(response);
            System.out.println(response);
            String token = jo.getString("data");
            System.out.println("create token : " + user.getMobileNumber());

            String row = user.getMobileNumber()+","+token;
            raf.seek(raf.length());
            raf.write(row.getBytes());
            raf.write("\r\n".getBytes());
            System.out.println("write to file : " + user.getMobileNumber());
        }
        raf.close();

        System.out.println("over");
    }
    public static Connection getConn() throws ClassNotFoundException, SQLException {
        String url = "jdbc:mysql://101.43.194.89:3306/seckill?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true&useSSL=false";
        String username= "seckill";
        String password= "ktEGHZ2R3eerhMAa";
        String drive = "com.mysql.cj.jdbc.Driver";
        Class.forName(drive);
        return DriverManager.getConnection(url,username,password);
    }
}
